package cn.edu.ncu.housinghub.service.impl;

import cn.edu.ncu.housinghub.constant.ErrorCodeConstant;
import cn.edu.ncu.housinghub.entity.appointment.Appointment;
import cn.edu.ncu.housinghub.exception.BusinessException;
import cn.edu.ncu.housinghub.mapper.AppointmentMapper;
import cn.edu.ncu.housinghub.service.AppointmentService;
import cn.edu.ncu.housinghub.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public Result createAppointment(Appointment appointment) {
        try {
            // 检查是否已存在未完成的预约
            List<Appointment> existingAppointments = appointmentMapper.getExistingAppointments(
                    appointment.getTenantId(),
                    appointment.getHouseId());

            // 如果存在未完成或未被拒绝的预约，则不允许再次预约
            for (Appointment existing : existingAppointments) {
                // 如果预约未被拒绝且未完成，则不允许再次预约
                if (existing.getIsRefused() == null || !existing.getIsRefused()) {
                    return Result.error(ErrorCodeConstant.CREATE_APPOINTMENT_ERROR.getCode(),
                            "您已存在对该房源的预约，请勿重复预约");
                }
            }

            appointmentMapper.insertAppointment(appointment);
            return Result.success("成功预约");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ErrorCodeConstant.CREATE_APPOINTMENT_ERROR.getCode(), "预约信息状态错误");
        }
    }

    @Override
    public Result getAppointmentListByTenantId(Long tenantId) {
        List<Appointment> appointmentList = appointmentMapper.getAppointmentListByTenantId(tenantId);
        if (appointmentList.isEmpty()) {
            return Result.success("暂无预约记录");
        } else {
            return Result.success(appointmentList);
        }
    }

    @Override
    public List<Appointment> getAgentPendingAppointments(Long agentUserId) {
        try {
            return appointmentMapper.selectAgentAppointmentsByStatus(agentUserId, 0); // 0=待处理
        } catch (Exception e) {
            throw new BusinessException(ErrorCodeConstant.SYSTEM_ERROR, "获取待处理预约失败");
        }
    }

    @Override
    public List<Appointment> getAgentConfirmedAppointments(Long agentUserId) {
        try {
            return appointmentMapper.selectAgentAppointmentsByStatus(agentUserId, 1); // 1=已确认
        } catch (Exception e) {
            throw new BusinessException(ErrorCodeConstant.SYSTEM_ERROR, "获取已确认预约失败");
        }
    }

    @Override
    public List<Appointment> getAgentCompletedAppointments(Long agentUserId) {
        try {
            return appointmentMapper.selectAgentAppointmentsByStatus(agentUserId, 2); // 2=已完成
        } catch (Exception e) {
            throw new BusinessException(ErrorCodeConstant.SYSTEM_ERROR, "获取已完成预约失败");
        }
    }

    @Override
    public List<Appointment> getAgentAllAppointments(Long agentUserId) {
        try {
            return appointmentMapper.selectAllAgentAppointments(agentUserId);
        } catch (Exception e) {
            throw new BusinessException(ErrorCodeConstant.SYSTEM_ERROR, "获取所有预约失败");
        }
    }

    @Override
    public void confirmAppointment(Long appointmentId, Long agentUserId) {
        try {
            // 验证预约是否属于该中介
            Appointment appointment = appointmentMapper.selectAppointmentById(appointmentId);
            if (appointment == null) {
                throw new BusinessException(ErrorCodeConstant.APPOINTMENT_NOT_FOUND);
            }

            // 更新预约状态为已确认
            appointmentMapper.updateAppointmentStatusByCode(appointmentId, 1, null);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessException(ErrorCodeConstant.SYSTEM_ERROR, "确认预约失败");
        }
    }

    @Override
    public void rejectAppointment(Long appointmentId, Long agentUserId, String reason) {
        try {
            // 验证预约是否属于该中介
            Appointment appointment = appointmentMapper.selectAppointmentById(appointmentId);
            if (appointment == null) {
                throw new BusinessException(ErrorCodeConstant.APPOINTMENT_NOT_FOUND);
            }

            // 更新预约状态为已拒绝
            appointmentMapper.updateAppointmentStatusByCode(appointmentId, -1, reason); // -1=已拒绝
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessException(ErrorCodeConstant.SYSTEM_ERROR, "拒绝预约失败");
        }
    }
}
